package com.demo.web.wx;

import com.demo.dto.BaseResult;
import com.demo.dto.ListResult;
import com.demo.dto.Pagination;
import com.demo.entity.SalePerson;
import com.demo.service.ISalePersonService;
import com.demo.vo.SalePersonVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/wx/saleperson")
public class SalePersonWxController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private ISalePersonService depService;
    
    /**
     * 创建
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/get", method = RequestMethod.GET)
    public BaseResult<SalePerson> get(SalePerson arg) {
    	SalePerson service_res = depService.selectByPrimaryKey(arg.getId());
        BaseResult<SalePerson> res = new BaseResult<SalePerson>(true, service_res);
        return res;
    }
//
    /**
     * login
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/login", method = RequestMethod.POST)
    public BaseResult<SalePerson> login(SalePerson arg,HttpSession session) {
        SalePerson service_res = depService.selectByPhone(arg);
        BaseResult<SalePerson> res;
        if(service_res!=null){
            session.setAttribute("current",service_res);
            res = new BaseResult<SalePerson>(true, service_res);
        }else {
            res = new BaseResult(false, "用户不存在",503);
        }
        return res;
    }

    /**
     * 修改头像
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update-thumb", method = RequestMethod.POST)
    public BaseResult<Integer> updateThumb(SalePerson arg, HttpSession session) {
        SalePerson sale = (SalePerson)session.getAttribute("current");
        BaseResult<Integer> res;
        if(sale!=null){
            arg.setId(sale.getId());
            int service_res = depService.updateByPrimaryKeySelective(arg);
            res = new BaseResult<Integer>(true, service_res);
            SalePerson sp = depService.selectByPrimaryKey(arg.getId());
            session.setAttribute("current",sp);
        }else {
            res = new BaseResult<Integer>(false,"请登录!",401);
        }
        return res;
    }

    /***
     * 修改数据
     * @param arg
     * @param session
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public BaseResult<Integer> update(SalePerson arg, HttpSession session) {
        SalePerson sale = (SalePerson)session.getAttribute("current");
        BaseResult<Integer> res;
        if(sale!=null){
            arg.setId(sale.getId());
            int service_res = depService.updateByPrimaryKeySelective(arg);
            res = new BaseResult<Integer>(true, service_res);
            SalePerson sp = depService.selectByPrimaryKey(arg.getId());
            session.setAttribute("current",sp);

        }else {
            res = new BaseResult<Integer>(false,"请登录!",401);
        }
        return res;
    }
    @ResponseBody
    @RequestMapping(value = "/current", method = RequestMethod.GET)
    public BaseResult<SalePerson> current(SalePerson arg, HttpSession session) {

        SalePerson sale = (SalePerson)session.getAttribute("current");
        BaseResult<SalePerson> res;
        if(sale!=null){
            res = new BaseResult<SalePerson>(true, sale);
        }else {
            res = new BaseResult(false,"请登录!",401);
        }
        return res;
    }

    /***
     * 分页查询
     * @param arg
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/query", method = RequestMethod.GET)
    public ListResult<SalePerson> query(SalePersonVo arg) {
        List<SalePerson> list = depService.queryByPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
    @ResponseBody
    @RequestMapping(value = "/mySales", method = RequestMethod.GET)
    public ListResult<SalePerson> queryMySales(SalePersonVo arg) {
        List<SalePerson> list = depService.queryByUserIdAndPage(arg);
        int total = depService.queryCount(arg);
        Pagination page = new Pagination(arg.getPage(), total, arg.getSize());
        ListResult<SalePerson> res = new ListResult(true, list);
        res.setPage(page);
        return res;
    }
}
